package com.irun.pm.ui;

import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Callable;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.controller.EnforcementControllers;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.mode.Enforcement;
import com.irun.pm.ui.CarQueryActivity.CardAdapter;
import com.irun.pm.ui.CarQueryActivity.ViewHolder;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-26
 * @email huangsanm@gmail.com
 * @version 1.0
 * @description 现场执法
 */
public class EnforcementActivity extends BaseActivity implements OnClickListener {

	private Button mQuery;
	private Button mAdd;
	private EditText mEnforceName;
	private EditText mFrom;
	private EditText mTo;
	private ListView mListView;
	private LayoutInflater mInflater;
	
	private EnforcementControllers mControllers;
	private List<Enforcement> mList;
	private EnforcementAdapter mAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enforcement_list);
		mInflater = (LayoutInflater) LayoutInflater.from(this);
		mControllers = new EnforcementControllers(this);
		
		mQuery = (Button) findViewById(R.id.query);
		mQuery.setOnClickListener(this);
		mAdd = (Button) findViewById(R.id.btnAdd);
		mAdd.setOnClickListener(this);
		mEnforceName = (EditText) findViewById(R.id.enforce_name);
		mFrom = (EditText) findViewById(R.id.date_from);
		mFrom.setOnClickListener(this);
		mTo = (EditText) findViewById(R.id.date_to);
		mTo.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.enforcement_query);
		
		initTableData(null, null ,null);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.query:
				String name = mEnforceName.getText().toString();
				String date_from = mFrom.getText().toString();
				String date_to = mTo.getText().toString();
				initTableData(name, date_from, date_to);
				/*doProgress("提示", "正在搜索中...", new Callable<String>() {
					@Override
					public String call() throws Exception {
						Thread.sleep(2000);
						return null;
					}
				}, new Callback<String>() {
					@Override
					public void onCallback(String pCallbackValue) {
						String name = mEnforceName.getText().toString();
						String date_from = mFrom.getText().toString();
						String date_to = mTo.getText().toString();
						initTableData(name, date_from, date_to);
						if(!mList.isEmpty()){
							Toast.makeText(EnforcementActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(EnforcementActivity.this, "查询不到任何数据", Toast.LENGTH_SHORT).show();
						}
						mAdapter = new EnforcementAdapter(mList);
						mListView.setAdapter(mAdapter);
						mFrom.setText(null);
						mTo.setText(null);
					}
				}, null);*/
				break;
			case R.id.date_from:
				getDatePickerDialogForDate(mFrom);
				break;
			case R.id.date_to:
				getDatePickerDialogForDate(mTo);
				break;
			case R.id.btnAdd:
				Intent intent = new Intent(this, SaveEnforcementActivity.class);
				startActivityForResult(intent, Utils.ENFORCEMENT_REQUEST_CODE);
				break;
		}
	}
	
	private void initTableData(final String name, final String date_from, final String date_to){
		doProgress("提示", "请稍等,正在请求数据...", new Callable<String>() {
			@Override
			public String call() throws Exception {
				Thread.sleep(2000);
				return null;
			}
		}, new Callback<String>() {
			@Override
			public void onCallback(String pCallbackValue) {
				mList = mControllers.getEnforcementList(name, date_from, date_to, Utils.TYPE_SITE_QUERY + "");
				/*if(mList == null || mList.size() == 0){
					Toast.makeText(EnforcementActivity.this, "没有符合条件的数据！", Toast.LENGTH_LONG).show();
				}else{*/
					mAdapter = new EnforcementAdapter(mList);
					mListView.setAdapter(mAdapter);
					mListView.setOnItemClickListener(new ListView.OnItemClickListener(){
						@Override
						public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
							Intent intent = new Intent(EnforcementActivity.this, EnforcementDetailActivity.class);
							System.out.println("id:" + mList.get(position).getId());
							intent.putExtra("id", mList.get(position).getId());
							startActivity(intent);
						}
					});
				//}
			}
		}, null);
	}
	
	/**
	 * 适配器
	 * @author huangsm
	 */
	private class EnforcementAdapter extends BaseAdapter {
		private List<Enforcement> elist;
		
		public EnforcementAdapter(List<Enforcement> list){
			elist = list;
		}

		@Override
		public int getCount() {
			return elist.size();
		}

		@Override
		public Object getItem(int position) {
			return elist.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(EnforcementActivity.this)
						.inflate(R.layout.car_item, null);
				holder = new ViewHolder();
				holder.tvCardNum = (TextView) convertView
						.findViewById(R.id.tvCardNum);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);
				holder.tvNmae = (TextView) convertView
						.findViewById(R.id.tvName);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Enforcement e = elist.get(position);
			holder.tvCardNum.setText(e.getNativeAddress());
			holder.tvTime.setText(e.getDate());
			holder.tvNmae.setText(e.getPlace());
			return convertView;
		}
		
		class ViewHolder {
			TextView tvCardNum;
			TextView tvTime;
			TextView tvNmae;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == Utils.ENFORCEMENT_REQUEST_CODE){
			if(resultCode == Utils.ENFORCEMENT_RESULT_CODE){
				initTableData(null, null, null);
				/*mAdapter = new EnforcementAdapter(mList);
				mListView.setAdapter(mAdapter);*/
			}
		}
	}
	
	/**
	 * 获取日期
	 * @author huangsm
	 * @date 2012-5-26
	 * @email huangsanm@gmail.com
	 * @description
	 */
	private void getDatePickerDialogForDate(final EditText et){
		final Calendar cal = Calendar.getInstance();
		int y = cal.get(Calendar.YEAR);
		int mOfy = cal.get(Calendar.MONTH);
		int dOfm = cal.get(Calendar.DAY_OF_MONTH);
		DatePickerDialog dpd = new DatePickerDialog(this, new OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				et.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
			}
		}, y, mOfy, dOfm);
		dpd.show();
	}
	
	
}
